﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class CoursewareDown : System.Web.UI.Page
{
    db_courseware cw = new db_courseware();
    db_course course = new db_course();

    public bool isLogin = false; //是否登录
    public bool isSell = false; //是否购买

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null)
        {
            Response.Redirect("index.aspx");
        }
        DataSet ds = new DataSet();
        string coursewareID = Request.QueryString["id"];

        string stuid = "";
        //判断是否登录
        try
        {
            if (Session["stuId"] != null)
            {
                isLogin = true;
                stuid = Session["stuId"].ToString();
            }
            else
            {
                isLogin = false;
            }
        }
        catch
        {
            isLogin = false;
        }

        //判读是否购买
        if (isLogin)
        {
            
            ds = cw.selectCoursewareByCourseID(coursewareID);
            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script language=javascript>alert('找不到数据！');</script>");
                Response.End();
            }
            else
            {
                isSell = course.CourseisSell(stuid, ds.Tables[0].Rows[0]["courseId"].ToString());
                //如果购买就显示课件下载地址
                if (isSell)
                {
                    string coursewareName = ds.Tables[0].Rows[0]["coursewareName"].ToString();
                    string coursewareUrl = ds.Tables[0].Rows[0]["coursewareUrl"].ToString();
                    string courseName = ds.Tables[0].Rows[0]["courseName"].ToString();
                    string courseId = ds.Tables[0].Rows[0]["courseId"].ToString();
                    this.Page.Title = coursewareName + "_" + courseName + "_课件信息";
                    if (File.Exists(Server.MapPath(coursewareUrl))) //判断课件文件是否存在
                    {
                        if (cw.coursewareDown(stuid, coursewareID))
                        {
                            Response.Redirect(coursewareUrl);
                        }
                        else
                        {
                            Response.Write("下载失败！");
                        }
                    }
                    else
                    {
                        Response.Write("Error，课件文件不存在。");
                    }
                }
                else
                {
                    Response.Write("<script language=javascript>alert('请购买相应的课程后再尝试！');</script>");
                    Response.End();
                }
            }
        }
        else
        {
            Response.Write("<script language=javascript>alert('请登录后再尝试');</script>");
            Response.End();
        }
    }
}